package tree.medium;

import tree.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * N 叉树的层序遍历
 *
 */
public class NAryTreeLevelOrderTraversal_429 {

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int len = queue.size();
            List<Integer> li = new ArrayList<>();
            while (len-- > 0) {
                Node poll = queue.poll();
                li.add(poll.val);
                for (Node child : poll.children) {
                    queue.offer(child);
                }
            }
            list.add(li);
        }
        return list;
    }
}
